package com.kgc.business.businessmanagerservice.service;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.business.bean.Users;
import com.kgc.business.bean.UsersExample;
import com.kgc.business.businessmanagerservice.mapper.UsersMapper;
import com.kgc.business.service.UserService;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    UsersMapper usersMapper;
    @Override
    public Map<String,Object> dologin(String name, String password) {
        UsersExample example=new UsersExample();
        UsersExample.Criteria criteria = example.createCriteria();
        Map<String,Object> map=new HashMap<>();
        if(!"".equals(name)){
        criteria.andNameEqualTo(name);
            List<Users> users = usersMapper.selectByExample(example);
            if(users.size()>0&&users!=null){
            Users users1=users.get(0);
            if(password.equals(users1.getPassword())){
            if(users1.getStatus()==0){
                map.put("msg","此账户已被冻结!");
            }else{
                map.put("msg",users1);
            }
            }else{
                map.put("msg","密码输入错误!");
            }
            }else{
                map.put("msg","用户名不存在!");
            }
        }
        return map;
    }

    @Override
    public Users seleById(Integer id) {
        return usersMapper.selectByPrimaryKey(id);
    }

    @Override
    public String seleUsers(String name,Integer pageNum) {
        PageHelper.startPage(pageNum,1);
        PageInfo<Users> pageInfo=null;
        if(!"".equals(name)){
            UsersExample example=new UsersExample();
            UsersExample.Criteria criteria = example.createCriteria();
            criteria.andNameLike("%"+name+"%");
            List<Users> users = usersMapper.selectByExample(example);
            pageInfo=new PageInfo<>(users);
        }else{
            List<Users> users = usersMapper.selectByExample(null);
            pageInfo=new PageInfo<>(users);
        }
        String s=JSONObject.toJSONString(pageInfo, SerializerFeature.WriteMapNullValue,SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteDateUseDateFormat);
        return s;
    }

    @Override
    public String getUsers(String password) {
        UsersExample example=new UsersExample();
        UsersExample.Criteria criteria = example.createCriteria();
        criteria.andPasswordEqualTo(password);
        List<Users> users = usersMapper.selectByExample(example);
        if(users!=null&&users.size()>0){
            return "true";
        }
        return "fail";
    }

    @Override
    public Integer updateUsers(Users users) {
        return usersMapper.updateByPrimaryKeySelective(users);
    }
}
